<?php
namespace app\model;
use think\Model;

class User extends Model
{
    // protected $table = 'user';
    // protected $autoWriteTimestamp = true;

    public function addAll($data){
        try {
            $data = $this->insertAll($data);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    //    return $this->where('work_num',$num)->find();
    }


    public function getInfoByWorkNum($num){
        try {
            $data = $this->where('work_num',$num)->find();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    //    return $this->where('work_num',$num)->find();
    }

    public function getInfoByDepartment($department){
        try {
            $data = $this->where('department',$department)->select();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    //    return $this->where('work_num',$num)->find();
    }

    public function getAll($list_rows,bool $simple = false,$config,$post){
        $select_post = array_diff_key ( $post, ['list_rows'=>-1,'page'=>-1] );
        try {
            $data = $this->where($select_post)->paginate($list_rows,$simple,$config)->toArray();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    //    return $this->where('work_num',$num)->find();
    }

    // public function getTotal(){
    //     try {
    //         $list = $this->order('work_num' ,'desc')->paginate(10);
    //         // 获取总记录数
    //         $count = $list->total();
    //         return [true,$count];
    //         //code...
    //     } catch (\Exception $e) {
    //         return [false,$e->getMessage()];
    //         //throw $th;
    //     }
    // //    return $this->where('work_num',$num)->find();
    // }

    public function getAllWorkNum(){
        try {
            $data = $this->withAttr('work_num')->select();
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }

    public function updateRoles($num,$roles){
        try {
            $roles = 6 - $roles;
            $data = $this->where('work_num', $num)->update(['roles' => $roles]);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }

    public function updateStatus($num,$status){
        try {
            // $status = 6 - $status;
            $flag = 0;
            if($status ==='已激活'){
                $flag = 1;
            }elseif($status === '已锁定'){
                $flag = 2;
            }else{
                return[false,$status];
            }
            // return $status;
            $data = $this->where('work_num', $num)->update(['status' => $flag]);
            return [true,$data];
            //code...
        } catch (\Exception $e) {
            return [false,$e->getMessage()];
            //throw $th;
        }
    }
}
